package com.uam.core.test;

import org.locationtech.jts.geom.*;

public class AddIntersectionPointToPolygon {
    public static void main(String[] args) {
        // 创建一个几何工厂
        GeometryFactory geometryFactory = new GeometryFactory();

        // 定义面的顶点
        Coordinate[] polygonCoordinates = new Coordinate[]{
                new Coordinate(1, 1),
                new Coordinate(1, 4),
                new Coordinate(4, 4),
                new Coordinate(4, 1),
                new Coordinate(1, 1) // 多边形应该是封闭的
        };
        Polygon polygon = geometryFactory.createPolygon(polygonCoordinates);

        // 创建要添加的交叉线段
        Coordinate[] intersectionLineCoords = new Coordinate[]{
                new Coordinate(0, 0),
                new Coordinate(5, 5)
        };
        LineString intersectionLine = geometryFactory.createLineString(intersectionLineCoords);

        // 获取面的边界
        Geometry polygonBoundary = polygon.getBoundary();

        // 将交叉线段与面的边界进行交集操作，以添加交叉点
        Geometry updatedBoundary = polygonBoundary.union(intersectionLine);

        // 更新面
        Geometry updatedPolygon = polygon.difference(polygonBoundary).union(updatedBoundary);

        // 打印更新后的面
        System.out.println("Updated Polygon: " + updatedPolygon);
    }
}

